package com.party.mobile.web.controller.mall;

import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallShowCategoryWithGoods;
import com.party.core.model.mall.MallShowGoodsWithCategory;
import com.party.core.model.mall.MallShowSubject;
import com.party.core.model.system.TargetType;
import com.party.core.service.mall.IMallShowGoodsService;
import com.party.core.service.mall.IMallShowSubjectService;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 展示商品控制器
 * Created by wei.li
 *
 * @date 2017/8/24 0024
 * @time 17:01
 */

@Controller
@RequestMapping(value = "mall/show/goods")
public class MallShowGoodsController {

    @Autowired
    private IMallShowGoodsService mallShowGoodsService;

    @Autowired
    private IMallShowSubjectService mallShowSubjectService;


    /**
     * 商品列表
     *
     * @param mallShowCategoryWithGoods 商品
     * @param page                      分页信息
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public AjaxResult list(MallShowCategoryWithGoods mallShowCategoryWithGoods, Page page) {
        AjaxResult ajaxResult = new AjaxResult();
        List<MallShowCategoryWithGoods> list = mallShowGoodsService.withCategoryListPage(mallShowCategoryWithGoods, page);
        ajaxResult.setData(list);
        ajaxResult.setPage(page);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * @param templateId: 专题模板编号
     * @param subjectId:  机构专题编号
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "platList")
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult platList(String templateId, String subjectId, Page page) {
        try {
            if (StringUtils.isEmpty(templateId) && StringUtils.isEmpty(subjectId)) {
                return AjaxResult.error(99999, "templateId和subjectId不能同时为空");
            }
            List<MallShowGoodsWithCategory> list = mallShowGoodsService.platList(subjectId, templateId, page);

            // 去掉商品的url前缀
            list.forEach(category -> {
                String url = category.getUrl();
                String newUrl = null;
                if (url.indexOf("http:") > -1) {
                    newUrl = url.substring("http:".length());
                } else if (url.indexOf("https:") > -1) {
                    newUrl = url.substring("https:".length());
                }
                if (StringUtils.isNotEmpty(newUrl) && !url.equals(newUrl)) {
                    String targetId = category.getTargetId();
                    Integer type = category.getType();
                    if (StringUtils.isNotEmpty(targetId) && null != type && StringUtils.isNotEmpty(subjectId)) {
                        MallShowSubject mallShowSubject = mallShowSubjectService.get(subjectId);
                        String memberId = mallShowSubject.getMemberId();
                        if (StringUtils.isNotEmpty(memberId)) {
                            newUrl += "?disId=" + memberId;
                        }
                    }
                    category.setUrl(newUrl);
                }
            });

            AjaxResult result = new AjaxResult();
            result.setSuccess(true);
            result.setData(list);
            result.setPage(page);
            return result;
        } catch (Exception e) {
            return AjaxResult.error(00000, "获取导购页数据异常");
        }
    }
}
